const db = uniCloud.database();
const userDB = db.collection("users");
const { doSqlMiddle } = require("utils");

async function createUser(data) {
  return doSqlMiddle(async () => {
    let user = await userDB.add(data);
    return user;
  });
}

async function getUser(id) {
  return doSqlMiddle(async () => {
    const user = await userDB.doc(id).get();
    const userData = user.data.length > 0 ? user.data[0] : {};
    delete userData.password;
    return userData;
  });
}

async function searchUser(params) {
  return doSqlMiddle(async () => {
    const res = await userDB.where(params).get();
    return res.data || [];
  });
}

module.exports = {
  createUser,
  getUser,
  searchUser,
};
